module GroRoutine

using GroParser
using Utils

function gro_functions(gro, grofilename::String)
    while true
        println(h("\n                    ************ GRO functions ************", fc="c"))
        println("-1 View summary of current structure")
        println(" 0 Return")
        println(" 1 Output to gro file")
        println(" 2 Append other structure files")
        input = sel_fun(Int32)
        if input == 0
            return
        elseif input == -1
            show_gro_summary(gro)
        elseif input == 1
            generate_gro(gro, grofilename)
        elseif input == 2
            append_gro(gro)
        end
    end
end

function show_gro_summary(gro::GRO)
    title_hint("Summary of current gro:\n")
    println("Title: $(gro.title)")
    println("Atom number: $(gro.atom_num)")
    println("Box info: $(gro.box)")
end

function generate_gro(gro::GRO, grofilename::AbstractString)
    gro_file = get_path(grofilename)
    to_gro(gro, gro_file)
    finish_path(gro_file)
end

function append_gro(gro::GRO)
    request_hint("Input gro file names to append, blank line to stop:\n")
    in_gros = Vector{String}()
    while true
        i = get_input("")
        if isempty(i)
            break
        end
        push!(in_gros, i)
    end
    for i in in_gros
        new_gro = GroParser.gen_gro_from_file(i)
        gro.atom_num += new_gro.atom_num
        append!(gro.atoms, new_gro.atoms)
    end
    finish_hint("Append gro files finished.\n")
end

end